package com.luoye.restful;

import com.luoye.restful.generator.FileGenerator;
import com.luoye.restful.generator.StringGenerator;
import com.luoye.restful.model.Api;
import com.luoye.restful.model.ApiDataDetail;
import com.luoye.restful.model.ApiDataField;
import com.luoye.restful.model.ApiGroup;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
@Data
public class DocBuilder {
    private String docTitle;
    private String docDes;
    private List<ApiGroup> apiGroupList;

    private String templatePath;
    private String targetPath;

    public DocBuilder() {
    }

    public DocBuilder(String docTitle, String docDes, List<ApiGroup> apiGroupList) {
        this.docTitle = docTitle;
        this.docDes = docDes;
        this.apiGroupList = apiGroupList;
    }

    public void build(){
        if(templatePath==null||templatePath.equals("")){
            templatePath= "template/";
        }
        if(targetPath==null||targetPath.equals("")){
            targetPath="apiDoc.md";
        }

        //生成接口组
        StringBuffer apiGroupListBuilder=new StringBuffer();
        StringGenerator groupGenerator=new StringGenerator();
        for (ApiGroup apiGroup:apiGroupList) {
            groupGenerator.clearReplaceStrs();
            groupGenerator.addReplaceStr("apiGroupTitle",apiGroup.getApiGroupTitle());
            groupGenerator.addReplaceStr("apiGroupDes",apiGroup.getApiGroupDes());
            //生成接口
            StringBuilder apiStrBuilder=new StringBuilder();
            StringGenerator apiGenerator=new StringGenerator();
            for (Api api:apiGroup.getApiList()) {
                apiGenerator.clearReplaceStrs();
                apiGenerator.addReplaceStr("apiTitle", api.getApiTitle());
                apiGenerator.addReplaceStr("apiDes", api.getApiDes());
                apiGenerator.addReplaceStr("apiUrl", api.getApiUrl());
                apiGenerator.addReplaceStr("apiMethod", api.getApiMethod());
                StringGenerator dataGenerator=new StringGenerator();
                //生成请求数据
                dataGenerator.clearReplaceStrs();
                dataGenerator.addReplaceStr("apiDataFormat", api.getRequestData().getApiDataFormat());
                dataGenerator.addReplaceStr("apiDataDetail", generateDataDetail(api.getRequestData().getDataDetailList()));
                apiGenerator.addReplaceStr("apiRequestData", dataGenerator.generate(templatePath + "api_data.md"));
                //生成响应数据
                dataGenerator.clearReplaceStrs();
                dataGenerator.addReplaceStr("apiDataFormat", api.getResponseData().getApiDataFormat());
                dataGenerator.addReplaceStr("apiDataDetail", generateDataDetail(api.getResponseData().getDataDetailList()));
                apiGenerator.addReplaceStr("apiResponseData", dataGenerator.generate(templatePath + "api_data.md"));
                String apiStr = apiGenerator.generate(templatePath + "api.md");
                apiStrBuilder.append(apiStr);
            }
            groupGenerator.addReplaceStr("apiList",apiStrBuilder.toString());

            String groupStr=groupGenerator.generate(templatePath+"api_group.md");
            apiGroupListBuilder.append(groupStr);
        }

        System.out.println("=================\n"+apiGroupListBuilder.toString());
        System.out.println("=================");

        FileGenerator fileGenerator=new FileGenerator();
        //生成文档
        String templatePathTemp= templatePath+"doc.md";
        String targetPathTemp=this.targetPath;
        fileGenerator.addReplaceStr("docTitle",docTitle);
        fileGenerator.addReplaceStr("docDes",docDes);
        fileGenerator.addReplaceStr("apiGroupList",apiGroupListBuilder.toString());

        fileGenerator.generate(templatePathTemp,targetPathTemp);
    }

    //生成数据详情
    private String generateDataDetail(List<ApiDataDetail> dataDetailList){
        //生成数据详情
        StringBuilder dataDetailStrBuilder=new StringBuilder();
        StringGenerator dataDetailGenerator=new StringGenerator();
        for (ApiDataDetail apiDataDetail:dataDetailList){
            String dataDetailStr ="";
            if(apiDataDetail.getEntityName()!=null&&!apiDataDetail.getEntityName().equals("")){
                dataDetailStr=apiDataDetail.getEntityName()+":";
            }
            if(apiDataDetail.getApiDataFieldList()==null||apiDataDetail.getApiDataFieldList().isEmpty()){
                dataDetailStr+=apiDataDetail.getEntityType()+"\n";
            }else {
                dataDetailGenerator.clearReplaceStrs();
                dataDetailGenerator.addReplaceStr("entityName", apiDataDetail.getEntityType());
                dataDetailGenerator.addReplaceStr("fieldList", generateFieldList(apiDataDetail.getApiDataFieldList()));
                dataDetailStr += dataDetailGenerator.generate(templatePath + "data_detail.md");
            }
            dataDetailStrBuilder.append(dataDetailStr);
        }
        return dataDetailStrBuilder.toString();
    }
    //生成字段详情
    private String generateFieldList(List<ApiDataField> dataFieldList){
        //生成数据详情
        StringBuilder dataFieldStrBuilder=new StringBuilder();
        StringGenerator dataFieldGenerator=new StringGenerator();
        for (ApiDataField apiDataField:dataFieldList){
            dataFieldGenerator.clearReplaceStrs();
            dataFieldGenerator.addReplaceStr("fieldName",apiDataField.getFieldName());
            dataFieldGenerator.addReplaceStr("fieldType",apiDataField.getFieldType());
            dataFieldGenerator.addReplaceStr("fieldMust",apiDataField.getFieldMust());
            dataFieldGenerator.addReplaceStr("fieldDes",apiDataField.getFieldDes());
            String dataFieldStr=dataFieldGenerator.generate(templatePath+"data_field.md");
            dataFieldStrBuilder.append(dataFieldStr);
        }
        return dataFieldStrBuilder.toString();
    }

    public void scan(){

    }



    public static void main(String[] args) {
        List<ApiGroup> apiGroupList=new ArrayList<ApiGroup>();
        ApiGroup apiGroup=new ApiGroup();
        apiGroup.setApiGroupTitle("用户管理");
        apiGroup.setApiGroupDes("ssdssss");
        Api api=new Api();
        api.setApiDes("sdfsdfsf");
        api.setApiTitle("添加用户");
        api.setApiMethod("dfdfdfdfdf");
        api.setApiUrl("http://{dfsfsf}:8080/sdfsdf/dff");
        api.getRequestData().setApiDataFormat("JSON");
        List<ApiDataField> fields=new ArrayList<ApiDataField>();
        fields.add(new ApiDataField("dddd","String","否","名字字段"));
        fields.add(new ApiDataField("dddd","String","否","名字字段"));
        api.getRequestData().getDataDetailList().add(new ApiDataDetail("eeee",fields));
        api.getResponseData().setApiDataFormat("JSON");
        api.getResponseData().getDataDetailList().add(new ApiDataDetail("eeee",fields));
        List<Api> apiList=new ArrayList<Api>();
        apiList.add(api);
        apiList.add(api);
        apiGroup.setApiList(apiList);
        apiGroupList.add(apiGroup);
        apiGroup=new ApiGroup();
        apiGroup.setApiGroupTitle("消息管理");
        apiGroup.setApiGroupDes("ssdssdfsfsdfsfsfsss");
        apiGroupList.add(apiGroup);
        new DocBuilder("api文档","apidfdsf",apiGroupList).build();
    }
}
